package edu.gatech.foodaway;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;



public class FoodPostActivity extends Activity {
	TextView txt_rname;
	ImageView img_rpicture;
	Button btn_foodlist;
	Button btn_addFoodItem;
	ListView list_foodPosts;
	Button btn_confirmPost;
	ArrayAdapter<String> arrayAdapter;
	ArrayList<String> currentPost = new ArrayList<String>();
	
	static final int SELECT_PREVIOUS_REQUEST = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foodpost);   
        txt_rname = (TextView) findViewById(R.id.textView_foodpost_restaurantname);
    	img_rpicture = (ImageView) findViewById(R.id.imageView_foodpost_restaurantpic);
    	btn_foodlist = (Button) findViewById(R.id.btn_previousitems) ;
    	btn_addFoodItem = (Button) findViewById(R.id.btn_additem);
    	list_foodPosts  = (ListView) findViewById(R.id.listView_foodpost_postingitems);
    	btn_confirmPost = (Button) findViewById(R.id.btn_confirm);
    	
    	btn_foodlist.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent(FoodPostActivity.this, PreviousItemsActivity.class);
				startActivityForResult(i, SELECT_PREVIOUS_REQUEST);
				
			}
    		
    	});
    	
    	btn_addFoodItem.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent(FoodPostActivity.this, AddItemActivity.class);
				startActivity(i);
				
			}
    		
    	});
    	
    	btn_confirmPost.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent(FoodPostActivity.this, ReviewActivity.class);
				startActivity(i);
				
			}
    		
    	});
    	arrayAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, currentPost);
    	list_foodPosts.setAdapter(arrayAdapter);
	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	  super.onActivityResult(requestCode, resultCode, data);
	  
	  if (requestCode == SELECT_PREVIOUS_REQUEST) {
          if (resultCode == RESULT_OK) {
             //populate list
        	  Intent i = getIntent();
        	  Bundle b = data.getExtras();
        	  currentPost.addAll(b.getStringArrayList(PreviousItemsActivity.RESULT_PREVIOUS));
        	  arrayAdapter.notifyDataSetChanged();
          }
      }
	}
}